//
//  AIGirlProfileEditViewController.swift
//  AIGirl
//
//  Created by Rae on 2023/12/29.
//

import UIKit
import ZLPhotoBrowser

class AIGirlProfileEditViewController: AIGirlBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("AIGirl_Signin_back").end()
    private let iconButton = c_make_ex(UIImageView.self)
        .corner(45.0).end()
    private let cameraImg = c_make_ex(UIImageView.self)
        .image("AIGirl_Profile_camera").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color(.white).alignment(.center).font(17.font_medium)
        .text("What should we call you?").end()
    private lazy var nameTextField = c_make_ex(UITextField.self)
        .bgColor("#F2F2FD".toColor.alpha(0.2)).corner(22)
        .color(.white).font(14.font_medium).alignment(.center)
        .placeholder("Enter your nickname").end()
    private lazy var nextButton = c_make_ex(AIGirlSubmitButton.self)
        .color(.white).font(21.font_bold).corner(20)
        .text("Next").end()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            iconButton
            cameraImg
            titleLabel
            nameTextField
            nextButton
        }
        
        backButton.leading(15).top(.statusHeight).size(30)
        iconButton.size(90).top(.navi_height + 33.0).centerHorizontally()
        cameraImg.size(25)
        cameraImg.Trailing == iconButton.Trailing
        cameraImg.Bottom == iconButton.Bottom - 6
        titleLabel.Top == iconButton.Bottom + 35
        titleLabel.centerHorizontally()
        nameTextField.leading(37.5).trailing(37.5).height(44)
        nameTextField.Top == titleLabel.Bottom + 20
        nextButton.leading(37.5).trailing(37.5).height(50)
        nextButton.Top == nameTextField.Bottom + 75
        
        if let img = GlobalTool.userModel?.headImage, img.count > 0 {
            iconButton.loadImage(url: img)
        }else{
            iconButton.image = "AIGirl_Default_Avatar".toImage
        }
        nameTextField.placehold("#B6AFC4".toColor, font: 14.font_medium)
        nameTextField.addTarget(self, action: #selector(editUserName), for: .editingChanged)
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        iconButton.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(clickIconButton))
        iconButton.addGestureRecognizer(tap)
        nextButton.addTarget(self, action: #selector(clickNextButton), for: .touchUpInside)
        nameTextField.text = GlobalTool.userModel?.name
    }
    
    @objc private func editUserName(_ textField: UITextField) {
        guard let text = textField.text else { return }
        if text.count > 20 {
            textField.text = (text as NSString).substring(to: 20)
        }
    }

    @objc private func clickIconButton(){
        ZLPhotoConfiguration.default()
            .allowSelectVideo(false)
        let ps = ZLPhotoPreviewSheet()
        ps.selectImageBlock = { [weak self] results, isOriginal in
            let image = results.first?.image
            self?.request.upload(image: image, isAvatar: true, completion: { result in
                if result.isSuccessful {
                    "Success".toast_success
                    GlobalTool.refreshUserInfo()
                    self?.iconButton.image = image
                }
            })
        }
        ps.showPhotoLibrary(sender: self)
        
    }
    
    @objc private func clickNextButton(){
        guard let nameStr = nameTextField.text, nameStr.count > 0 else {
            "Please enter your nickname!".toast_error
            return
        }
        
        if nameTextField.text?.count ?? 0 > 20 {
            "Nickname cannot exceed 20 characters".toast_warning
            return
        }
        self.request.start(.RegisterInfo(name: nameStr, age: 18, gender: 1)) { result in
            if result.isSuccessful {
                UserDefaultsManager.setString(nameStr, forKey: .userName)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }

}
